//
//  UIView+ShowInView.m
//  acfun_video
//
//  Created by user on 11/25/15.
//  Copyright © 2015 wang. All rights reserved.
//

#define UICOLOR_MAKE_RGBINT(rgbInt) UICOLOR_MAKE_RGBINT_A(rgbInt,1)
#define UICOLOR_MAKE_RGBINT_A(rgbInt,a) [UIColor colorWithRed:((float)((rgbInt & 0xFF0000) >> 16))/255.0 green:((float)((rgbInt & 0xFF00) >> 8))/255.0 blue:((float)(rgbInt & 0xFF))/255.0 alpha:a]

#import "UIView+ShowInView.h"
#import <objc/runtime.h>
#import "UIView+Utilities.h"
static char* ACShowInViewContainerViewTag                  = "ACShowInViewContainerViewTag";
static char* ACShowInViewMaskViewTag                       = "ACShowInViewMaskViewTag";
static char* ACShowInViewIsShowingTag                      = "ACShowInViewIsShowingTag";
static char* ACShowInViewDidDismissFromContainerTag        = "ACShowInViewDidDismissFromContainerTag";

@interface UIView (ShowInViewUtil)
///
@property (nonatomic, weak)     UIView  *containerView;
@property (nonatomic, weak)     UIView  *maskView;

@property (nonatomic, assign)   BOOL    isShowing;

@end

@implementation UIView(ShowInViewUtil)

- (void)setContainerView:(UIView *)container {
    objc_setAssociatedObject(self, ACShowInViewContainerViewTag, container, OBJC_ASSOCIATION_ASSIGN);
}

- (UIView *)containerView {
    return objc_getAssociatedObject(self, ACShowInViewContainerViewTag);
}

- (void)setMaskView:(UIView *)maskView {
    objc_setAssociatedObject(self, ACShowInViewMaskViewTag, maskView, OBJC_ASSOCIATION_ASSIGN);
}

- (UIView *)maskView {
    return objc_getAssociatedObject(self, ACShowInViewMaskViewTag);
}
- (void)setIsShowing:(BOOL)isShowing {
    objc_setAssociatedObject(self, ACShowInViewIsShowingTag, @(isShowing), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isShowing {
    return [objc_getAssociatedObject(self, ACShowInViewIsShowingTag) boolValue];
}

@end

@implementation UIView (ShowInView)

- (void)setDidDismissFromContainerBlock:(dispatch_block_t)didDismissFromContainerBlock {
    objc_setAssociatedObject(self, ACShowInViewDidDismissFromContainerTag, didDismissFromContainerBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}


- (dispatch_block_t)didDismissFromContainerBlock {
    return objc_getAssociatedObject(self, ACShowInViewDidDismissFromContainerTag);
}

- (void)showInContainerView:(UIView *)containerView maskView:(UIView *)maskView maskFrame:(CGRect)maskFrame targetFrame:(CGRect)targetFrame {
    self.isShowing = YES;
    self.containerView = containerView;
    self.maskView = maskView;
    
    maskView.frame = maskFrame;
    maskView.alpha = 0.0;
    [containerView addSubview:maskView];
    self.frame = targetFrame;
    ///从顶部出现
    self.top = containerView.top - self.height;
    self.alpha = 0.0;
    [containerView addSubview:self];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissMaskView:)];
    [maskView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:.3 animations:^{
        self.maskView.alpha = 1.;
        self.alpha = 1.;
        self.frame = targetFrame;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showInContainerView:(UIView *)containerView targetTop:(CGFloat)targetTop leftMargin:(CGFloat)leftMargin {
    UIView *maskView = [[UIView alloc] initWithFrame:containerView.bounds];
    maskView.backgroundColor = [UICOLOR_MAKE_RGBINT(0x515151) colorWithAlphaComponent:0.5];
    CGRect maskFrame = maskView.frame;
    CGRect targetFrame = CGRectMake(leftMargin, targetTop, self.width, self.height);
    [self showInContainerView:containerView maskView:maskView maskFrame:maskFrame targetFrame:targetFrame];
}

- (void)showInContainerView:(UIView *)containerView targetTop:(CGFloat)targetTop {
    UIView *maskView = [[UIView alloc] initWithFrame:containerView.bounds];
    maskView.backgroundColor = [UICOLOR_MAKE_RGBINT(0x515151) colorWithAlphaComponent:0.5];
    CGRect maskFrame = maskView.frame;
    CGRect targetFrame = CGRectMake(0, targetTop, self.width, self.height);
    [self showInContainerView:containerView maskView:maskView maskFrame:maskFrame targetFrame:targetFrame];
}

- (void)dismissMaskView:(UITapGestureRecognizer *)tap {
    if (self.didDismissFromContainerBlock) {
        self.didDismissFromContainerBlock();
    }
    [UIView animateWithDuration:.3 animations:^{
        self.maskView.alpha = 0.;
        self.top = - self.height;
    } completion:^(BOOL finished) {
        self.isShowing = NO;
        [self.maskView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)dismissFromContainer {
    [self dismissMaskView:nil];
}

// 注释掉，是因为这样写会造成 死循环！
//- (BOOL)isShowing {
//    return self.isShowing;
//}
@end
